package databaseVersionControl.infra.script.validation;

import java.lang.reflect.Method;

import databaseVersionControl.infra.annotation.DatabaseScript;
import databaseVersionControl.infra.annotation.ScriptAuthor;
import databaseVersionControl.infra.annotation.ScriptOrder;

public class ScriptValidation {

	public static boolean isScriptClass(Class<?> clazz) {
		return clazz.isAnnotationPresent(DatabaseScript.class) && hasAnyScriptMethod(clazz);
	}

	public static boolean isValidNumber(Class<?> clazz, Integer number) {
		for (Method method : clazz.getMethods()) {
			if (!method.isAnnotationPresent(ScriptOrder.class))
				continue;
			if (method.getAnnotation(ScriptOrder.class).value() == number)
				return true;
		}
		return false;
	}

	public static boolean hasAnyScriptMethod(Class<?> clazz) {
		for (Method method : clazz.getMethods()) {
			if (isScriptMethod(method))
				return true;
		}
		return false;
	}

	public static boolean isScriptMethod(Method method) {
		return method.isAnnotationPresent(ScriptOrder.class) && method.isAnnotationPresent(ScriptAuthor.class);
	}

}